#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from app import db
from datetime import datetime


class DictCategory(db.Model):
    """字典分类表"""
    __tablename__ = 'dict_categories'
    
    id = db.Column(db.Integer, primary_key=True, comment='主键ID')
    dict_name = db.Column(db.String(100), nullable=False, comment='字典名称')
    dict_code = db.Column(db.String(50), nullable=False, unique=True, comment='字典编码')
    sort_order = db.Column(db.Integer, default=0, comment='排序序号')
    remark = db.Column(db.Text, comment='备注说明')
    is_active = db.Column(db.Boolean, default=True, comment='是否有效')
    create_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, comment='更新时间')
    
    # 关联字典项
    dict_items = db.relationship('DictItem', backref='category', lazy='dynamic', cascade='all, delete-orphan')
    
    def to_dict(self):
        """转换为字典格式"""
        return {
            'id': self.id,
            'dict_name': self.dict_name,
            'dict_code': self.dict_code,
            'sort_order': self.sort_order,
            'remark': self.remark,
            'is_active': self.is_active,
            'item_count': self.dict_items.count(),
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S') if self.create_time else None,
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S') if self.update_time else None
        }
    
    def __repr__(self):
        return f'<DictCategory {self.dict_name}>'


# 添加Dictionary类作为DictCategory的别名
Dictionary = DictCategory


class DictItem(db.Model):
    """字典项表"""
    __tablename__ = 'dict_items'
    
    id = db.Column(db.Integer, primary_key=True, comment='主键ID')
    dict_id = db.Column(db.Integer, db.ForeignKey('dict_categories.id'), nullable=False, comment='字典分类ID')
    item_name = db.Column(db.String(100), nullable=False, comment='项名称')
    item_value = db.Column(db.String(100), nullable=False, comment='数据值')
    sort_order = db.Column(db.Integer, default=0, comment='排序序号')
    is_active = db.Column(db.Boolean, default=True, comment='是否有效')
    remark = db.Column(db.Text, comment='备注说明')
    create_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, comment='更新时间')
    
    # 唯一约束：同一字典分类下，项名称唯一
    __table_args__ = (
        db.UniqueConstraint('dict_id', 'item_name', name='uk_dict_item_name'),
        db.UniqueConstraint('dict_id', 'item_value', name='uk_dict_item_value'),
    )
    
    def to_dict(self):
        """转换为字典格式"""
        return {
            'id': self.id,
            'dict_id': self.dict_id,
            'dict_code': self.category.dict_code if self.category else None,
            'dict_name': self.category.dict_name if self.category else None,
            'item_name': self.item_name,
            'item_value': self.item_value,
            'sort_order': self.sort_order,
            'is_active': self.is_active,
            'remark': self.remark,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S') if self.create_time else None,
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S') if self.update_time else None
        }
    
    def __repr__(self):
        return f'<DictItem {self.item_name}>' 